import { put, takeEvery, call } from "redux-saga/effects"
import { apiGetPostList } from "../../apis/posts"

function* getPosts(action) {
  try {
    const res = yield call(apiGetPostList, action.params)
    // remove 'content'
    const data = res.map(({ content, ...rest }) => ({ ...rest }))
    yield put({ type: "post-store/update-posts", data })
  } catch (err) {
    console.error(err)
  }
}

export default function*() {
  yield takeEvery("post-store/get-posts", getPosts)
}
